package com.attendancesystem.controller;


import com.attendancesystem.service.UnitService;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import javax.servlet.annotation.WebServlet;
import java.io.PrintWriter;
import java.io.Writer;

@WebServlet(name = "DeleteUnitController", value = "/DeleteUnitController")
public class DeleteUnitController extends HttpServlet {
    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse rep){
        rep.setCharacterEncoding("UTF-8");
        rep.setContentType("application/json;charset=UTF-8");

        try {
            PrintWriter out = rep.getWriter();


            String unitId = req.getParameter("unitId");
            System.out.println("unitId:"+unitId);
            UnitService unitService = new UnitService();
            if (unitService.deleteUnit(Integer.parseInt(unitId))){
                out.print("{\"status\":\"success\"}");
            }else {
                out.print("{\"status\":\"failed\"}");
            }
        }catch (Exception e){
            rep.setStatus(400);
        }

    }
}
